﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TimeAxis.Interfaces;
using TimeAxis.Models;

namespace TimeAxis.Impls
{
    public class ImplUsers : IUsers
    {
        private DavidDB.ADO.DBManager dbmanager = null;


        #region 返回接口实现

        private ImplUsers(DavidDB.ADO.DBManager db) { this.dbmanager = db; }

        public static IUsers GetInstance(DavidDB.ADO.DBManager db)
        {
            IUsers instance = new ImplUsers(db);
            return instance;
        }
        #endregion
        #region 默认接口实现

        /// <summary>
        /// 得到所有 用户表在大项目中尽量少用，这个方法返回的数据量过于庞大
        /// </summary>
        /// <returns>所有用户表</returns>
        public List<Users> GetList()
        {
            Users field = new Users();
            var objs = dbmanager.GetList(field);
            List<Users> fields = new List<Users>();
            foreach (Users _field in objs)
            {
                fields.Add(_field);
            }
            return fields;
        }

        /// <summary>
        /// 分页显示用户表
        /// </summary>
        /// <param name="PageSize">每页记录数</param>
        /// <param name="NowPage">当前页</param>
        /// <param name="PageCount">总共多少页</param>
        /// <param name="recordCount">总记录数</param>
        /// <returns>查询到的记录</returns>
        public List<Users> GetListByPage(int PageSize, int NowPage, out int PageCount, out int recordCount)
        {
            Users field = new Users();
            var objs = dbmanager.GetListByPage(PageSize, NowPage, out PageCount, out recordCount, field);
            List<Users> fields = new List<Users>();
            foreach (Users _field in objs)
            {
                fields.Add(_field);
            }
            return fields;
        }

        /// <summary>
        /// 分页模糊查询用户表
        /// </summary>
        /// <param name="users">对象实体</param>
        /// <param name="PageSize">每页记录数</param>
        /// <param name="NowPage">当前页</param>
        /// <param name="PageCount">总共多少页</param>
        /// <param name="recordCount">总记录数</param>
        /// <returns>查询到的记录</returns>
        public List<Users> GetListByPageLikeKey(Users users, int PageSize, int NowPage, out int PageCount, out int recordCount)
        {
            var objs = dbmanager.GetListByPageLikeKey(PageSize, NowPage, users, out PageCount, out recordCount);
            List<Users> fields = new List<Users>();
            foreach (Users _field in objs)
            {
                fields.Add(_field);
            }
            return fields;
        }

        /// <summary>
        /// 分页查询用户表
        /// </summary>
        /// <param name="users">对象实体</param>
        /// <param name="PageSize">每页记录数</param>
        /// <param name="NowPage">当前页</param>
        /// <param name="PageCount">总共多少页</param>
        /// <param name="recordCount">总记录数</param>
        /// <returns>查询到的记录</returns>
        public List<Users> GetListByPageAndKeys(Users users, int PageSize, int NowPage, out int PageCount, out int recordCount)
        {
            var objs = dbmanager.GetListByPageAndKeys(PageSize, NowPage, users, out PageCount, out recordCount);
            List<Users> fields = new List<Users>();
            foreach (Users _field in objs)
            {
                fields.Add(_field);
            }
            return fields;
        }

        /// <summary>
        /// 得到一个用户表
        /// </summary>
        /// <param name="id">对象实体</param>
        /// <returns>对象</returns>
        public Users GetModel(int id)
        {
            Users field = new Users();
            field.Id = id;
            return dbmanager.GetModel(field) as Users;
        }

        /// <summary>
        /// 保存一个用户表
        /// </summary>
        /// <param name="users">对象实体</param>
        /// <returns>编号</returns>
        public int Insert(Users users)
        {
            return dbmanager.Insert(users);
        }

        /// <summary>
        /// 按关键字查询头几条
        /// </summary>
        /// <param name="count">数量</param>
        /// <returns>用户表的列表</returns>
        public List<Users> GetTopList(int count)
        {
            Users field = new Users();
            var objs = dbmanager.GetTopList(count, field);
            List<Users> fields = new List<Users>();
            foreach (Users _field in objs)
            {
                fields.Add(_field);
            }
            return fields;
        }

        /// <summary>
        /// 判断用户表是否唯一存在
        /// </summary>
        /// <param name="users">对象实体</param>
        /// <returns>是否存在</returns>
        public bool IsExist(Users users)
        {
            return dbmanager.IsExist(users);
        }

        /// <summary>
        /// 判断用户表是否存在
        /// </summary>
        /// <param name="users">对象实体</param>
        /// <returns>是否存在</returns>
        public bool IsJustExist(Users users)
        {
            return dbmanager.IsJustExist(users);
        }

        /// <summary>
        /// 得到关键字等于。。。的所有用户表
        /// </summary>
        /// <param name="users">对象实体</param>
        /// <returns>用户表的List</returns>
        public List<Users> GetSomeObject(Users users)
        {
            var objs = dbmanager.GetSomeObject(users);
            List<Users> fields = new List<Users>();
            foreach (Users _field in objs)
            {
                fields.Add(_field);
            }
            return fields;
        }

        /// <summary>
        /// 编辑一个用户表
        /// </summary>
        /// <param name="obj">对象实体</param>
        public void Edit(Users users)
        {
            dbmanager.Edit(users);
        }

        /// <summary>
        /// 删除一个用户表
        /// </summary>
        /// <param name="users">对象实体</param>
        public void Delete(Users users)
        {
            dbmanager.Delete(users);
        }

        /// <summary>
        /// 删除所有用户表
        /// </summary>
        public void DeleteAll()
        {
            Users field = new Users();
            dbmanager.DeleteAll(field);
        }

        /// <summary>
        /// 按关键字删除
        /// </summary>
        /// <param name="users">对象实体</param>
        public void DeleteColumByKey_Value(Users users)
        {
            dbmanager.DeleteColumByKey_Value(users);
        }

        #endregion

        #region 自定义接口实现


        #endregion



        public Users Login(string username, string password)
        {
            Users user = new Users();
            user.Password = password;
            user.Username = username;
            var query = dbmanager.GetSomeObject(user);
            if (query.Count() == 0)
            {
                return null;
            }
            else
            {
                return query.FirstOrDefault();
            }
        }
    }
}
